{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Counter({'a': 3, 'g': 2, 'b': 1})\n",
      "Counter({'a': 3, 'g': 2, 'b': 1})\n",
      "Counter({'a': 3, 'g': 2, 'b': 1})\n"
     ]
    }
   ],
   "source": [
    "import collections\n",
    "\n",
    "print(collections.Counter(['a', 'b', 'g', 'g', 'a', 'a']))\n",
    "print(collections.Counter({'a':3, 'g':2, 'b':1}))\n",
    "print(collections.Counter(a=3, g=2, b=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Initial:  Counter()\n",
      "Counter now:  Counter({'r': 1, 't': 1, 'e': 1, 'y': 1, 'u': 1, 'i': 1})\n",
      "After update:  Counter({'t': 5, 'i': 3, 'r': 1, 'e': 1, 'y': 1, 'u': 1})\n"
     ]
    }
   ],
   "source": [
    "# Initialize empty counter container and populate with update()\n",
    "\n",
    "cnt = collections.Counter()\n",
    "print('Initial: ', cnt)\n",
    "\n",
    "cnt.update('rteyui')\n",
    "print('Counter now: ', cnt)\n",
    "\n",
    "cnt.update({'t':4, 'i':2})\n",
    "print('After update: ', cnt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, 'w': 1, 'r': 1, 'd': 1, 't': 0})\n",
      "\n",
      "['h', 'e', 'l', 'l', 'l', 'o', 'o', 'w', 'r', 'd']\n"
     ]
    }
   ],
   "source": [
    "# Accessing elements of counter\n",
    "\n",
    "\n",
    "c = collections.Counter('helloworld')\n",
    "c['t'] = 0\n",
    "print(c)\n",
    "print()\n",
    "print(list(c.elements()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('l', 3), ('o', 2), ('h', 1), ('e', 1), ('w', 1), ('r', 1), ('d', 1), ('t', 0)]\n"
     ]
    }
   ],
   "source": [
    "# print most common element and its count in the counter\n",
    "print(c.most_common())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('l', 3), ('o', 2), ('h', 1)]\n",
      "\n",
      "[('l', 3), ('o', 2)]\n"
     ]
    }
   ],
   "source": [
    "print(c.most_common(3))\n",
    "print()\n",
    "print(c.most_common(2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a1:  Counter({'h': 2, 'l': 2, 'o': 2, 'e': 1, 'p': 1, 'y': 1, 't': 1, 'n': 1})\n",
      "\n",
      "a2:  Counter({'h': 2, 'l': 2, 'o': 2, 'e': 1, 'c': 1, 'y': 1, 't': 1, 'n': 1})\n",
      "\n",
      "Addition:  Counter({'h': 4, 'l': 4, 'o': 4, 'e': 2, 'y': 2, 't': 2, 'n': 2, 'p': 1, 'c': 1})\n",
      "\n",
      "Subtraction:  Counter({'p': 1})\n",
      "\n",
      "Intersection:  Counter({'h': 2, 'l': 2, 'o': 2, 'e': 1, 'y': 1, 't': 1, 'n': 1})\n",
      "\n",
      "Union:  Counter({'h': 2, 'l': 2, 'o': 2, 'e': 1, 'p': 1, 'y': 1, 't': 1, 'n': 1, 'c': 1})\n"
     ]
    }
   ],
   "source": [
    "#Counter arithmetic\n",
    "\n",
    "a1 = collections.Counter('hellopython')\n",
    "a2 = collections.Counter('hellocython')\n",
    "\n",
    "print('a1: ', a1)\n",
    "print()\n",
    "print('a2: ', a2)\n",
    "print()\n",
    "\n",
    "# addition\n",
    "print('Addition: ', a1+a2)\n",
    "print()\n",
    "\n",
    "# subtraction\n",
    "print('Subtraction: ', a1-a2)\n",
    "print()\n",
    "\n",
    "# Intersection\n",
    "print('Intersection: ', a1 & a2)\n",
    "print()\n",
    "\n",
    "#Union\n",
    "print('Union: ', a1 | a2)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
